
var loanamount;
var donepay;
var restInt;
var percent;
var allpri;
var fundType;
var dataRate;
var dataValue;
var _hlmonthRate = 0.0325;
var _bsmonthRate = 0.049;

			var currentCity = areaUtils.getCurrentCity();
			var cityIdentId = currentCity.identId;
			console.log(cityIdentId);
			var cityParam = [{
				key:'cityIdentId',
				value:cityIdentId
			}];
			
			fjNetwork.get('fjzx/common/house-rate.json',cityParam,function(data){
//				console.log(JSON.stringify(data));
				_hlmonthRate = data.houseRate.fundRate?data.houseRate.fundRate:0.0325;
				_bsmonthRate = data.houseRate.loanRate?data.houseRate.loanRate:0.049;
				document.querySelector('#publicFundRate span').innerHTML = '基准利率('+_hlmonthRate*100+'%)';
				document.querySelector('#businessFundRate span').innerHTML = '基准利率('+_bsmonthRate*100+'%)';
				document.querySelector('.public-rate').innerHTML = _hlmonthRate*100;
				document.querySelector('.business-rate').innerHTML = _bsmonthRate*100;
			});

			window.addEventListener('changeDonePay',function(event){
				donepay = parseInt(event.detail.donepay);
				percent = event.detail.txt;
				document.querySelector('#donePercent').innerHTML = percent;
				document.querySelector('#downPaymentNum').innerHTML = donepay;
				allpri= document.querySelector('#allPayment').innerHTML;
				loanamount =  loanAmount(allpri,donepay);

				document.querySelector('#loanamount').innerHTML = loanamount;
				
				document.querySelector('#houseFund').innerHTML = loanamount/2+'万';
				document.querySelector('#businessFund').innerHTML = loanamount/2+'万';

				document.querySelector('.downPaymentNum').innerHTML = donepay;//首付
				document.querySelector('.showLoanAll').innerHTML = loanamount;//贷款总额
				document.querySelector('.downPaymentNum2').innerHTML = donepay;//首付
				document.querySelector('.showLoanAll2').innerHTML = loanamount;//贷款总额
				getLoanSameInt(_hlmonthRate,_bsmonthRate);//等额本息
				
				
			});
			
			//开始计算  显示计算结果弹窗
			document.querySelector('#countPay').addEventListener('tap',function(){
				document.querySelector('#loanboxShadow').classList.remove('mui-hidden');
				document.querySelector('.loanListBox').classList.remove('mui-hidden');
				//同时计算等额本息
				document.querySelector('.downPaymentNum').innerHTML = donepay;//首付
				document.querySelector('.showLoanAll').innerHTML = loanamount;//贷款总额
				document.querySelector('.downPaymentNum2').innerHTML = donepay;//首付
				document.querySelector('.showLoanAll2').innerHTML = loanamount;//贷款总额
				
				getLoanSameInt(_hlmonthRate,_bsmonthRate);//等额本息
				restInt = document.querySelector('#allInterest').innerHTML;

			});
			document.querySelector('#loanboxShadow').addEventListener('tap',function(){
				this.classList.add('mui-hidden');
				document.querySelector('.loanListBox').classList.add('mui-hidden');
			});
			
			document.querySelector('#sameIntLoan').addEventListener('tap',function(){
				var clsName = this.classList;
				if(clsName.length==2){
				}else{
					getLoanSameInt(_hlmonthRate,_bsmonthRate);//等额本息
					restInt = document.querySelector('#allInterest').innerHTML;
				}
			});
			document.querySelector('#sameCapLoan').addEventListener('tap',function(){
				var clsName = this.classList;
				if(clsName.length==2){
				}else{
					getLoanSameMony(_hlmonthRate,_bsmonthRate);//等额本金
					restInt = document.querySelector('#allInterest2').innerHTML;
				}
			});

			mui.plusReady(function(){
				var wv = plus.webview.currentWebview();
				var allPayment = parseInt(wv.allPayment);
				console.log(allPayment);
				allpri = allPayment;
				document.querySelector('#allPayment').innerHTML = allPayment;
				document.querySelector('#downPaymentNum').innerHTML = parseInt(allPayment*0.4);
				document.querySelector('#loanamount').innerHTML = allPayment - parseInt(allPayment*0.4);
				loanamount = allPayment - parseInt(allPayment*0.4);
				donepay =document.querySelector('#downPaymentNum').innerHTML;
				document.querySelector('#toDownPayment').addEventListener('tap',function(){
					mui.openWindow({
						url:'down_payment.html',
						id:'down_payment',
						extras:{
							allPay:allPayment,//总贷款
							donePay:donepay
						}
					})
				});
				
				document.querySelector('#houseFund').innerHTML = loanamount/2+'万';
				document.querySelector('#businessFund').innerHTML = loanamount/2+'万';
				console.log(_hlmonthRate);
				//公积金贷款利率选择
				document.querySelector('#publicFundRate').addEventListener('tap',function(){
					fundType = 1;
					openWindowWithPage('loan_rate',{
						loanRate:_hlmonthRate
					});
				});
				//商业贷款利率选择
				document.querySelector('#businessFundRate').addEventListener('tap',function(){
					fundType = 2;
					openWindowWithPage('loan_rate',{
						loanRate:_bsmonthRate
					});
				});
				
				//自定义函数获取选择贷款利率
				document.addEventListener('getFundRate',function(event){
					dataRate = event.detail.dataRate;		//自定义利率
					dataValue = event.detail.dataValue;
					switch(fundType){
						case 1:
						document.querySelector('#publicFundRate span').innerHTML = dataValue+'('+dataRate+'%)';
						_hlmonthRate = (dataRate/100).toFixed(4);
						break;
						case 2:
						document.querySelector('#businessFundRate span').innerHTML = dataValue+'('+dataRate+'%)';
						_bsmonthRate = (dataRate/100).toFixed(4);
						break;
					}
				});
			
			
				mui('.showLoanDetail').each(function(){
					
					this.addEventListener('tap',function(){
						var id = this.getAttribute('id');
					openWindowWithPage('morgate_detail',{
						id:id,
						downPay:donepay,//首付
						allLoan:loanamount,//总贷款
						loanArr:allLoad,
						_housefundy:_housefundy,
						_businessfundy:_businessfundy,
						restInt:restInt
					});
					});
					
				});
				
				
				setsuData(loanamount);	//设置贷款选择项
			});
//	var allpri= document.querySelector('#allPayment').innerHTML;
	var downPay = document.querySelector('#downPaymentNum').innerHTML;

//贷款总额
	loanamount =  loanAmount(allpri,downPay);
//	console.log(allpri);
/*
 * 房贷计算，摘自iosSelect.js
 * 1、公积金贷款
 * 2、商业贷款*/

			//1、公积金贷款
(function(){
	var showGeneralDom = document.querySelector('#publicFunds');//公积金贷款
	var suIdDom = document.querySelector('#houseFund');
	var weiIdDom = document.querySelector('#houseFundY');
	
	showGeneralDom.addEventListener('tap', function () {
		
    var suId = showGeneralDom.dataset['su_id'];
    var suValue = showGeneralDom.dataset['su_value'];
    var weiId = showGeneralDom.dataset['wei_id'];
    var weiValue = showGeneralDom.dataset['wei_value'];
    var sanguoSelect = new IosSelect(2, 
        [suData, weiData],
        {
            title: '公积金贷款',
            itemHeight: 35,
            oneLevelId: suId,
            twoLevelId: weiId,
            callback: function (selectOneObj, selectTwoObj) {
                suIdDom.value = selectOneObj.id;
                weiIdDom.value = selectTwoObj.id;
                
                suIdDom.innerHTML = selectOneObj.value;
                weiIdDom.innerHTML = selectTwoObj.value;
                
                //对应商业贷款本金
                document.querySelector('#businessFund').innerHTML = parseFloat(loanamount - strGetNum(selectOneObj.value))+'万';
            }
		});
	});
})(mui);


//2、商业贷款
(function(){
	var showGeneralDom = document.querySelector('#businessLoan');//商业贷款
	var suIdDom = document.querySelector('#businessFund');
	var weiIdDom = document.querySelector('#businessFundY');
	
	showGeneralDom.addEventListener('tap', function () {
		
    var suId = showGeneralDom.dataset['su_id'];
    var suValue = showGeneralDom.dataset['su_value'];
    var weiId = showGeneralDom.dataset['wei_id'];
    var weiValue = showGeneralDom.dataset['wei_value'];
    var sanguoSelect = new IosSelect(2, 
        [businessData, businessDataY],
        {
            title: '商业贷款',
            itemHeight: 35,
            oneLevelId: suId,
            twoLevelId: weiId,
            callback: function (selectOneObj, selectTwoObj) {
                suIdDom.value = selectOneObj.id;
                weiIdDom.value = selectTwoObj.id;
                
                
                suIdDom.innerHTML = selectOneObj.value;
                weiIdDom.innerHTML = selectTwoObj.value;
                
                //对应公积金贷款本金
                document.querySelector('#houseFund').innerHTML = parseFloat(loanamount - strGetNum(selectOneObj.value))+'万'
            }
		});
		
	});
})(mui);

	document.querySelector('#loanamount').innerHTML = loanamount;
	document.querySelector('.showLoanAll').innerHTML = loanamount +'万元';	//开始计算显示的贷款总额
	
	/*
	 * 
	 * 开始计算*
	 *
	 */

	var houseFund,houseFundY;
	var _housefund,_housefundy;
	var businessFund,businessFundY;
	var _businessfund,_businessfundy;
	
function getLoanData(){
	//公积金贷款

		houseFund = document.querySelector('#houseFund').innerHTML;
	
		houseFundY = document.querySelector('#houseFundY').innerHTML;

	_housefund = strGetNum(houseFund)*10000;
	
	_housefundy = strGetNum(houseFundY);
	
	
	//商业贷款
	
		businessFund = document.querySelector('#businessFund').innerHTML;

		businessFundY = document.querySelector('#businessFundY').innerHTML;

		_businessfund = strGetNum(businessFund)*10000;
	
		_businessfundy = strGetNum(businessFundY);
	
}

	var allLoad = {		//所有的贷款总和数组
		'mPay':0,
		'mInt':0,
		'mCap':0
	};
	
//计算等额本息贷款
function getLoanSameInt(hlmonthRate,bsmonthRate){
//	console.log(hlmonthRate);
	getLoanData();
	
	var hlmonthRate = hlmonthRate;
	var bsmonthRate = bsmonthRate;
	/********等额本息贷款*********/
	//公积金
	var houseLoan = calculateLoan;
	

	houseLoan.setData(_housefund,hlmonthRate,_housefundy*12);
	var hlMonthpayment = houseLoan.monthPayment();//月供额
	var hlMonthInterest = houseLoan.monthInterest();//月利息，数组
	var hlMonthCapital = houseLoan.monthCapital();//月本金，数组
	var hlallCapital = houseLoan.allCapital();//总利息
	
	//商业
	var businessLoan = calculateLoan;
	
	businessLoan.setData(_businessfund,bsmonthRate,_businessfundy*12);
	var bsMonthpayment = businessLoan.monthPayment();//月供额
	var bsMonthInterest = businessLoan.monthInterest();//月利息，数组
	var bsMonthCapital = businessLoan.monthCapital();//月本金，数组
	var bsallCapital = businessLoan.allCapital();//总利息
	
	var allcapital = hlallCapital + bsallCapital;
	document.querySelector('#allInterest').innerHTML = Math.round(allcapital) + '元';

	
	var houseLoanData = {	//公积金贷款数组
		'mPay':1,
		'mInt':2,
		'mCap':3
	};
	var businessLoanData = {	//商业贷款数组
		'mPay':1,
		'mInt':2,
		'mCap':3
	};

	
	for (var i=0,j=0; i<_housefundy*12,j<_businessfundy*12; i++,j++) {
		houseLoanData[i] = {
				'mPay':hlMonthpayment,
				'mInt':hlMonthInterest[i],
				'mCap':hlMonthCapital[i]
			};
			businessLoanData[j] = {
				'mPay':bsMonthpayment,
				'mInt':bsMonthInterest[j],
				'mCap':bsMonthCapital[j]
			};
	}

	var gap = _housefundy - _businessfundy;	//年限差
	
	if(gap>0){	//公积金贷款时间长
		for (var i=0; i< _businessfundy*12; i++) {
			allLoad[i] = {
				'mPay':houseLoanData[i].mPay + businessLoanData[i].mPay,
				'mInt':houseLoanData[i].mInt + businessLoanData[i].mInt,
				'mCap':houseLoanData[i].mCap + businessLoanData[i].mCap
			}

		}
		for (var j =_businessfundy*12; j<_housefundy*12; j++) {
			allLoad[j] = {
				'mPay':houseLoanData[j].mPay,
				'mInt':houseLoanData[j].mInt,
				'mCap':houseLoanData[j].mCap
			}

		}
		
	}else if(gap==0){	//两种贷款时间一样
		for (var i=0; i< _businessfundy*12; i++) {
			allLoad[i] = {
				'mPay':houseLoanData[i].mPay + businessLoanData[i].mPay,
				'mInt':houseLoanData[i].mInt + businessLoanData[i].mInt,
				'mCap':houseLoanData[i].mCap + businessLoanData[i].mCap
			}


		}	
//		console.log(houseLoanData[0].mPay)
//		console.log(businessLoanData[0].mPay)
		
	}else if(gap<0){	//商业贷款时间长
		for (var i=0; i< _housefundy*12; i++) {
			allLoad[i] = {
				'mPay':houseLoanData[i].mPay + businessLoanData[i].mPay,
				'mInt':houseLoanData[i].mInt + businessLoanData[i].mInt,
				'mCap':houseLoanData[i].mCap + businessLoanData[i].mCap
			}

		}

		for (var j =_housefundy*12; j<_businessfundy*12; j++) {
			allLoad[j] = {
				'mPay':businessLoanData[j].mPay,
				'mInt':businessLoanData[j].mInt,
				'mCap':businessLoanData[j].mCap
			}

		}
		
	}

	document.querySelector('.monthPay').innerHTML = Math.round(allLoad[0].mPay);

}



//计算等额本金贷款
function getLoanSameMony(hlmonthRate,bsmonthRate){
	
	getLoanData();
	
	
	/********等额本金贷款*********/
	//公积金

	var hlmonthRate = hlmonthRate/12;

	//商业

	var bsmonthRate = bsmonthRate/12;

	
	//每月应还本金=贷款本金÷还款月数
	var hsMonthCap = _housefund / (_housefundy*12);//公积金 每月应还本金
	
	var bsMonthCap = _businessfund / (_businessfundy*12);//商业 每月应还本金
	
	
	
	//每月应还利息=剩余本金×月利率=(贷款本金-已归还本金累计额)×月利率
	var hsRestCap = _housefund;
	var hsMonthInt = [];
	
	//每月月供额=(贷款本金÷还款月数)+(贷款本金-已归还本金累计额)×月利率=每月应还本金+每月应还利息
	var hsMonthPay = [];
	var bsMonthPay = [];
	
	for (var i=0; i<_housefundy*12; i++) {//公积金 每月应还利息
			hsRestCap -= hsMonthCap;	//剩余本金
		if(hsRestCap<0){
			hsMonthInt[i]=0;
		}else{
			hsMonthInt[i] = hsRestCap * hlmonthRate;	//每月应还利息
		}
		
		
		hsMonthPay[i] = hsMonthCap + hsMonthInt[i];	//每月月供额
		
		
	}
	var bsRestCap = _businessfund;
	var bsMonthInt = [];
	for (var i=0; i<_housefundy*12; i++) {//商业 每月应还利息
		bsRestCap = bsRestCap-bsMonthCap;	//剩余本金
		
		if(bsRestCap<0){
			bsMonthInt[i] = 0;
			
		}else{
			bsMonthInt[i] = bsRestCap * bsmonthRate;
		}
		
//		bsMonthInt[i] = 0;	//每月应还利息
		
		bsMonthPay[i] = bsMonthCap + bsMonthInt[i];	//每月月供额
	}
	
	
		console.log(hsMonthPay[0]+','+bsMonthPay[0]);
	
	//每月月供递减额=每月应还本金×月利率=贷款本金÷还款月数×月利率
	var hsMonthDesMny = hsMonthCap * hlmonthRate;
	
	var bsMonthDesMny = bsMonthCap * bsmonthRate;
	
	document.querySelector('.monthPayDes').innerHTML = (hsMonthDesMny+bsMonthDesMny).toFixed(2)+'元';
	
	//总利息=（还款月数+1）*贷款额*月利率/2
	var hsAllInterest = (_housefundy*12+1) * _housefund * hlmonthRate /2;//利息总额
	var bsAllInterest = (_businessfundy*12+1) * _businessfund * bsmonthRate /2;
	
	document.querySelector('#allInterest2').innerHTML = Math.round(hsAllInterest+bsAllInterest) + '元';
	
	if(_housefundy > _businessfundy){	//公积金年限>商业年限
		for (var i=0; i<_businessfundy*12; i++) {
			allLoad[i] = {
				'mPay':	hsMonthPay[i] + bsMonthPay[i],//月供额
				'mInt': hsMonthInt[i] + bsMonthInt[i],//月利息
				'mCap': hsMonthCap + bsMonthCap		//月本金
			}
		}
		for (var j=_businessfundy*12; j<_housefundy*12; j++) {
			allLoad[j] = {
				'mPay':	hsMonthPay[j],//月供额
				'mInt': hsMonthInt[j],//月利息
				'mCap': hsMonthCap	//月本金
			}
		}
	}else if(_housefundy == _businessfundy){	//公积金年限==商业年限
		for (var i=0; i<_businessfundy*12; i++) {
			allLoad[i] = {
				'mPay':	hsMonthPay[i] + bsMonthPay[i],//月供额
				'mInt': hsMonthInt[i] + bsMonthInt[i],//月利息
				'mCap': hsMonthCap + bsMonthCap		//月本金
			}
		}
	}else if(_housefundy < _businessfundy){	//公积金年限<商业年限
		for (var i=0; i<_housefundy*12; i++) {
			allLoad[i] = {
				'mPay':	hsMonthPay[i] + bsMonthPay[i],//月供额
				'mInt': hsMonthInt[i] + bsMonthInt[i],//月利息
				'mCap': hsMonthCap + bsMonthCap		//月本金
			}
		}
		for (var j=_housefundy*12; j<_businessfundy*12; j++) {
			allLoad[j] = {
				'mPay':	bsMonthPay[j],//月供额
				'mInt': bsMonthInt[j],//月利息
				'mCap': bsMonthCap	//月本金
			}
		}
	}
	
	document.querySelector('.monthPay2').innerHTML = Math.round(allLoad[0].mPay);
	
	//详细信息显示列表
//	var maxLen = Math.max(_housefundy,_businessfundy);
//	var showMortDetail = document.querySelector('#showMortDetail');
//	var restLoan = loanamount*10000;


}

//贷款本金
function loanAmount(allPri,downPay){
	return (parseFloat(allPri)-parseFloat(downPay));
}

 /*
  * 
  * 计算等额本息贷款
  * 
  * */

var calculateLoan = (function(){
	
	var _loanamount,_monthRate,_month;
	/*
	 * _loanamout 贷款本金
	 * _mouthRate 月利率
	 * _month     还款月数
	 * */
	var setData = function(loanamount,monthRate,month){
	
		_loanamount = loanamount;
		_monthRate = monthRate/12;
		_month = month;
	};
	var rate = function(){	//(1＋月利率)＾还款月数
		var monthrate = 1+_monthRate;
		var rat = Math.pow(monthrate,_month);
//		console.log(rat);
		return	rat;		
	};
	var rate2 = function(){	//(1+月利率)^(还款月序号-1)
		var monthrate = 1+_monthRate;
		var rateArray = [];
		var rat;
		for(var i=0; i<_month; i++){
			rat = Math.pow(monthrate,i);
			rateArray[i]=rat;

		}
		return rateArray;
	};
	var monthPayment = function(){
		//每月月供额=〔贷款本金×月利率×(1＋月利率)＾还款月数〕÷〔(1＋月利率)＾还款月数-1〕
		var _rate = this.rate();
		var r = _loanamount * _monthRate * _rate;
		var mP = r / (_rate-1);
		return mP;
	};
	var monthInterest = function(){
		//每月应还利息=贷款本金×月利率×〔(1+月利率)^还款月数-(1+月利率)^(还款月序号-1)〕÷〔(1+月利率)^还款月数-1〕
		var rate = this.rate();
		var rate2 = this.rate2();
		var r = [],rat = [];
		var mInt=[];
		for (var i=0; i<_month; i++) {
//			rat[i] = Math.pow(monthrate,i);//(1+月利率)^(还款月序号-1)
			r[i] = rate - rate2[i];
			mInt[i] = _loanamount * _monthRate *r[i] /(rate-1);
		}
		return mInt;
	};
	var monthCapital = function(){
		//每月应还本金=贷款本金×月利率×(1+月利率)^(还款月序号-1)÷〔(1+月利率)^还款月数-1〕
		
		var capital = [];
		var r = this.rate2();
		var rate = this.rate();
		
		for(var i=0; i<r.length; i++){

			capital[i] = _loanamount * _monthRate * r[i]/(rate-1);

		}
		return capital;
	};
	var allCapital = function(){
		//总利息=还款月数×每月月供额-贷款本金
		var allcapital = _month * this.monthPayment() - _loanamount;
		return allcapital;
	};
	
	return{
		setData:setData,
		rate:rate,
		rate2:rate2,
		monthPayment:monthPayment,
		monthInterest:monthInterest,
		monthCapital:monthCapital,
		allCapital:allCapital
	}
})();



 //计算等额本息贷款

    //每月月供额=〔贷款本金×月利率×(1＋月利率)＾还款月数〕÷〔(1＋月利率)＾还款月数-1〕
    //每月应还利息=贷款本金×月利率×〔(1+月利率)^还款月数-(1+月利率)^(还款月序号-1)〕÷〔(1+月利率)^还款月数-1〕
    //每月应还本金=贷款本金×月利率×(1+月利率)^(还款月序号-1)÷〔(1+月利率)^还款月数-1〕
    //总利息=还款月数×每月月供额-贷款本金

//计算等额本金贷款

    //每月月供额=(贷款本金÷还款月数)+(贷款本金-已归还本金累计额)×月利率
    //每月应还本金=贷款本金÷还款月数
    //每月应还利息=剩余本金×月利率=(贷款本金-已归还本金累计额)×月利率
    //每月月供递减额=每月应还本金×月利率=贷款本金÷还款月数×月利率
    //总利息=（还款月数+1）*贷款额*月利率/2

/**********字符串提取数字********/
function strGetNum(str){
	return str.replace(/[^0-9]/ig,'');
}

//loanamount贷款总额
/*
 *贷款选择列表
 */
function setsuData(loanamount){
//console.log(loanamount);
var speed = 10;
suData.length = parseInt(loanamount/10);

for (var i=0; i<suData.length; i++) {
	suData[i] = {
		'id' : '100'+i,
		'value' : speed+'万'
	}
	
	businessData[i] = {
		'id' : 'b00'+i,
		'value' : speed+'万'
	}
	
	speed +=10;

}
weiData.length = 30;

for (var i=0; i<30; i++) {
	weiData[i] = {
		'id':'200'+i,
		'value':'年限'+(i+1)+'年'
	}
	
	businessDataY[i] = {
		'id':'b100'+i,
		'value':'年限'+(i+1)+'年'
	}

}
	
}